package com.qing.expert.modules.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.expert.common.response.R;
import com.qing.expert.modules.admin.entity.Announcement;
import com.qing.expert.modules.admin.service.AnnouncementService;
import com.qing.expert.modules.admin.vo.request.AnnouncementRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import com.qing.expert.common.annotation.OperationLog;

@Slf4j
@RestController
@RequestMapping("/api/admin/announcement")
public class AdminAnnouncementController {

    @Resource
    private AnnouncementService announcementService;

    /**
     * 创建通告
     */
    @OperationLog(action = "create_announcement", detail = "创建通告: #{#request.title}")
    @PostMapping
    public R<Void> createAnnouncement(@Validated @RequestBody AnnouncementRequest request) {
        announcementService.createAnnouncement(request);
        return R.ok();
    }

    /**
     * 更新通告
     */
    @OperationLog(action = "update_announcement", detail = "更新通告: #{#id}")
    @PutMapping("/{id}")
    public R<Void> updateAnnouncement(@PathVariable Long id, @Validated @RequestBody AnnouncementRequest request) {
        announcementService.updateAnnouncement(id, request);
        return R.ok();
    }

    /**
     * 删除通告
     */
    @OperationLog(action = "delete_announcement", detail = "删除通告: #{#id}")
    @DeleteMapping("/{id}")
    public R<Void> deleteAnnouncement(@PathVariable Long id) {
        announcementService.deleteAnnouncement(id);
        return R.ok();
    }

    /**
     * 分页获取通告列表
     */
    @GetMapping("/page")
    public R<Page<Announcement>> getAnnouncementPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return R.ok(announcementService.getAnnouncementPage(page, size));
    }
}